﻿module KeywordsTests


open System
open System.IO
open Microsoft.VisualStudio.TestTools.UnitTesting
open Streams
open Keywords
open Tokens


[<TestClass>]
type KeyWordsTests() =

   [<TestMethod>]
   member public self.IsKeyword_SucceedsWithValidInput() =
      use stream = CreateStream("Feature: Scenario: Given: When: Then:")
      use reader = CreateReader(stream)

      let recurse() =
         let keyword = ReadToken(reader)

         if (keyword.IsSome) then
            Assert.IsTrue(IsKeyword(keyword.Value))

      recurse()


   [<TestMethod>]
   member public self.IsKeyword_FailsWithInvalidInput() =
      use stream = CreateStream("Feature")
      use reader = CreateReader(stream)

      let keyword = ReadToken(reader).Value

      Assert.IsFalse(IsKeyword(keyword))


   [<TestMethod>]
   member public self.ReadKeyword_SucceedsWithValidInput() =
      use stream = CreateStream(" Feature: This is a feature description.")
      use reader = CreateReader(stream)

      let actual = ReadKeyword(reader, Keyword.Feature)

      Assert.IsTrue(actual.IsSome)


   [<TestMethod>]
   member public self.ReadKeyword_FailsWithInvalidInput() =
      use stream = CreateStream(" Feature: This is a feature description.")
      use reader = CreateReader(stream)

      let actual = ReadKeyword(reader, Keyword.Scenario)

      Assert.IsFalse(actual.IsSome)


   [<TestMethod>]
   member public self.TryReadKeyword_ResetsLocationAndReturnsTrueAfterSuccessfulRead() =
      use stream = CreateStream(" Scenario: This is a scenario description.")
      use reader = CreateReader(stream)

      let previous = reader.GetLocation()
      let actual = TryReadKeyword(reader, Keyword.Scenario)
      let current = reader.GetLocation()

      Assert.IsTrue(actual)   
      Assert.AreEqual(previous.Index, current.Index)


   [<TestMethod>]
   member public self.TryReadKeyword_ResetsLocationAndReturnsFalseAfterUnsuccessfulRead() =
      use stream = CreateStream(" Feature: This is a feature description.")
      use reader = CreateReader(stream)

      let previous = reader.GetLocation()
      let actual = TryReadKeyword(reader, Keyword.Scenario)
      let current = reader.GetLocation()

      Assert.IsFalse(actual)
      Assert.AreEqual(previous.Index, current.Index)